package com.example.springmvc.controller;

import com.example.springmvc.pojo.Item;
import com.example.springmvc.pojo.User;
import com.example.springmvc.service.IUserService;
import com.example.springmvc.vo.QueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/list")
    public String list(Model m) {
        List<User> users = userService.queryList();
        m.addAttribute("users", users);
        return "user/list";
    }

    @RequestMapping("/queryUser")
    public String queryItem(QueryVO queryVO)
    {
        System.out.println(queryVO.getUser().getId());
        System.out.println(queryVO.getUser().getUsername());

        System.out.println(Arrays.toString(queryVO.getIds()));

        for(User u:queryVO.getUserList())
        {
            System.out.println(u);
        }

        return "success";
    }

    @RequestMapping("/userEdit.action")
    public String userEdit(@RequestParam(value = "userId", required = true, defaultValue = "1") Integer id, Model model)
    {
        User user = userService.findById(id);
        model.addAttribute("user",user);

        return "/user/edit";
    }

    @RequestMapping("/updateUser.action")
    public String updateUserById(User user) {

        // 信息
        this.userService.updateById(user);

        System.out.println(user);

        return "forward:/user/userEdit.action?userId="+user.getId();
    }

    @RequestMapping("/login")
    public String login() {
        return "/user/login";
    }

    @RequestMapping("/loginchk")
    public String loginchk(String username, String password, HttpSession session) {
        System.out.println("username:"+username);

        session.setAttribute("username",username);

        return "redirect:/item/itemList2";
    }



}
